import axios from 'axios'
import Message from '@hi-ui/hiui/es/message'
// 创建axios实例
const http = axios.create({
    baseURL: "http://localhost:8088",
    // baseURL: "http://124.222.33.107:8088",
    timeout: 30000, // 请求超时时间
    withCredentials: true, // 是否跨站点访问控制请求
    headers: {
        'Content-Type': 'application/json;charset=UTF-8'
    }
})

// request拦截器
http.interceptors.request.use(
    (config) => {
        return config
    },
    (err) => {
        console.log('request err:' + err)
        return Promise.reject(err)
    }
)

// respone拦截器
http.interceptors.response.use(
    (response) => {
        console.log('ok', response)
        // if (response.data.code === 400) {
        // }
        return response
    },
    (err) => {
        console.log('error', err.response)
        if (err && err.response) {
            switch (err.response.status) {
                case 400:
                    err.message = '请求错误(400)'
                    break
                case 404:
                    Message.open({
                        type:'error',
                        title:'请求错误(404 Not Found)'
                    })
                    err.message = '请求错误(404)'
                    break
                default:
                    err.message = `连接出错(${err.response.status})!`
            }
        } else {
            err.message = '连接服务器失败!'
            window.location.href = './netWork.html'
        }

        return Promise.reject(err)
    }
)

export function httpGet (url, params = {}) {
    return new Promise((resolve, reject) => {
        http
            .get(url, {
                params:params
            })
            .then((res) => {
                console.log('res', res)
                if (res.status === 200) {
                    resolve(res.data)
                } else {
                    Message.open({
                        type:'error',
                        title:`error ${res.status}`
                    })
                    reject(res.data.message)
                }
            })
            .catch((err) => {
                reject(err)
            })
    })
}

// 耗时操作（异步）
export function httpGet1 (url, params = {}) {
    return new Promise((resolve, reject) => {
        http
            .get(url, {
                params:params,
                timeout: 60000
            })
            .then((res) => {
                console.log('res', res)
                if (res.status === 200) {
                    resolve(res.data)
                } else {
                    Message.open({
                        type:'error',
                        title:`error ${res.status}`
                    })
                    reject(res.data.message)
                }
            })
            .catch((err) => {
                reject(err)
            })
    })
}

export function httpPost (url, params = {}) {
    return new Promise((resolve, reject) => {
        http
            .post(url, params)
            .then((res) => {
                console.log('res', res)
                if (res.data.code === 200) {
                    resolve(res.data)
                } else {
                    Message.open({
                        type:'error',
                        title:res.data.message
                    })
                    reject(res.data.message)
                }
            })
            .catch((err) => {
                reject(err)
            })
    })
}
